import 'package:flutter/material.dart';

class DashedLine extends StatelessWidget {
  final Axis direction;
  final double dashGap;
  final double dashLength;
  final Color color;

  DashedLine({
    required this.direction,
    this.dashGap = 2.0,
    this.dashLength = 1.0,
    this.color = Colors.grey,
  });

  @override
  Widget build(BuildContext context) {
    return CustomPaint(
      painter: DashedLinePainter(
        direction: direction,
        dashGap: dashGap,
        dashLength: dashLength,
        color: color,
      ),
    );
  }
}

class DashedLinePainter extends CustomPainter {
  final Axis direction;
  final double dashGap;
  final double dashLength;
  final Color color;

  DashedLinePainter({
    required this.direction,
    required this.dashGap,
    required this.dashLength,
    required this.color,
  });

  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = color
      ..strokeWidth = direction == Axis.horizontal ? 1 : 1
      ..style = PaintingStyle.stroke;

    final space = dashGap + dashLength;

    if (direction == Axis.horizontal) {
      final startY = size.height / 2;
      var startX = 0.0;

      while (startX < size.width) {
        canvas.drawLine(
          Offset(startX, startY),
          Offset(startX + dashLength, startY),
          paint,
        );

        startX += space;
      }
    } else {
      final startX = size.width / 2;
      var startY = 0.0;

      while (startY < size.height) {
        canvas.drawLine(
          Offset(startX, startY),
          Offset(startX, startY + dashLength),
          paint,
        );

        startY += space;
      }
    }
  }

  @override
  bool shouldRepaint(DashedLinePainter oldDelegate) {
    return oldDelegate.direction != direction || oldDelegate.dashGap != dashGap || oldDelegate.dashLength != dashLength || oldDelegate.color != color;
  }
}
